package main

import (
	"fmt"
	"math"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cannot Sqrt negative number: %f",
		e)
}

func _sqrt(x float64) (z float64) {
	count := 0

	if x == 1 {
		z = 1
	} else {
		z = x / 2
		for {
			count++
			delta := (z*z - x) / (2 * z)
			if math.Abs(delta) > 0.000000001 {
				z -= delta
			} else {
				break
			}
		}
	}

	fmt.Printf("count:%d\n", count)
	return
}
func Sqrt(x float64) (float64, error) {
	if x < 0 {
		return 0, ErrNegativeSqrt(x)
	} else {
		return _sqrt(x), nil
	}
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(Sqrt(-2))
}
